package com.example.ziliao.utils

import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView

object OptionsHelper {
    fun generateAndDisplayOptions(
        correctAnswer: Double,
        useRangeOptions: Boolean,
        btnOptionA: Button,
        btnOptionB: Button,
        btnOptionC: Button,
        btnOptionD: Button,
        optionsContainer: LinearLayout,
        tvExactResult: TextView,
        resultPrefix: String = "准确结果"
    ) {
        // 根据题型选择生成选项的方式
        val options = if (useRangeOptions) {
            OptionGenerator.generateRangeOptions(correctAnswer)
        } else {
            OptionGenerator.generateValueOptions(correctAnswer)
        }

        // 设置选项按钮
        btnOptionA.text = "A. ${options[0].first}"
        btnOptionB.text = "B. ${options[1].first}"
        btnOptionC.text = "C. ${options[2].first}"
        btnOptionD.text = "D. ${options[3].first}"

        // 设置点击事件
        val buttons = listOf(btnOptionA, btnOptionB, btnOptionC, btnOptionD)
        buttons.forEachIndexed { index, button ->
            button.setOnClickListener {
                if (options[index].second) {
                    button.setBackgroundColor(0xFF4CAF50.toInt()) // 绿色
                } else {
                    button.setBackgroundColor(0xFFF44336.toInt()) // 红色
                    // 显示正确选项
                    options.forEachIndexed { correctIndex, (_, isCorrect) ->
                        if (isCorrect) {
                            buttons[correctIndex].setBackgroundColor(0xFF4CAF50.toInt()) // 绿色
                        }
                    }
                }
                buttons.forEach { it.isEnabled = false }
                
                // 显示准确结果
                tvExactResult.text = "$resultPrefix：${OptionGenerator.formatNumber(correctAnswer)}"
                tvExactResult.visibility = View.VISIBLE
            }
        }

        // 显示选项容器
        optionsContainer.visibility = View.VISIBLE
    }

    fun resetOptions(
        btnOptionA: Button,
        btnOptionB: Button,
        btnOptionC: Button,
        btnOptionD: Button,
        optionsContainer: LinearLayout,
        tvExactResult: TextView
    ) {
        // 重置按钮状态
        btnOptionA.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionB.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionC.setBackgroundColor(0xFFE0E0E0.toInt())
        btnOptionD.setBackgroundColor(0xFFE0E0E0.toInt())
        
        btnOptionA.isEnabled = true
        btnOptionB.isEnabled = true
        btnOptionC.isEnabled = true
        btnOptionD.isEnabled = true
        
        // 隐藏选项容器和结果文本
        optionsContainer.visibility = View.GONE
        tvExactResult.visibility = View.GONE
        tvExactResult.text = ""
    }
} 